$(document).ready(function(){
    $("#input-date").datepicker({ 
        buttonImage: base_url + '/images/calendar-icon.png',
        buttonImageOnly: true,
        showOn: 'button',
        altFormat: '@',
        onSelect:function(date, time) {
            var time_stamp = $(this).datepicker("getDate") / 1000;
            window.location.href = base_url + "/tab/index/index/pid/" + id_social_page + "/time/" + time_stamp;
        }
    });
    
    $(".left-menu").click(function() {
        offset = 0;
        $(".wrap-menu .active").removeClass("active");
        $(this).addClass("active");
        getTopFans();
    });
});

function getTopFans() {
    $(".loader").css("display", "block");
    var type = $(".wrap-menu .active").attr("name");
    $(".wrap-fans").html("");
    $.ajax({
        url: base_url + "/ajax/index/gettopfans",
        data: {
            pid: id_social_page,
            offset: offset,
            type: type,
            time: time
        },
        success:function(res) {
            $(".loader").css("display", "none");
            $(".wrap-fans").html(res);  
            offset += fan_per_page;
        }
    });
}

function viewMore() {
    $("#view-more-loader").css("display", "block");
    var type = $(".wrap-menu .active").attr("name");
    $("#wrap-view-more").html("");
    $.ajax({
        url: base_url + "/ajax/index/gettopfans",
        data: {
            pid: id_social_page,
            offset: offset,
            type: type,
            time: time
        },
        success:function(res) {
            $("#view-more-loader").remove();
            $("#wrap-view-more").replaceWith(res);
            offset += fan_per_page;
        }
    });
}

function showInstruction(event) {
    $(".wrap-menu .active").removeClass("active");
    $(event.target).addClass("active");
    $(".loader").css("display", "block");
    $.ajax({
        url: base_url + "/ajax/index/instruction",
        data: {
            pid: id_social_page
        },
        success:function(res) {
            $(".loader").css("display", "none");
            $(".wrap-fans").html(res);  
        }
    });
}

function vote(event, voted_id) {
    if (id_social_user == null) {
        FB.login(function(resp) {
            window.parent.location = "http://www.facebook.com/" + id_social_page + "?sk=app_" + appId;
        }, {scope: 'email'});
    } else {
        $.ajax({
            url: base_url + "/ajax/index/vote",
            data: {
                pid      : id_social_page,
                uid      : id_social_user,
                voted_id : voted_id,
                start_time: time,
                end_time: end_time
            },
            success:function(resp) {
                if (resp === '1') {
                    window.parent.location = "http://www.facebook.com/" + id_social_page + "?sk=app_" + appId;
                } else {
                    //handle error 
                }
            }
        });
    }
}

function unVote(event, vote_id) {
    $.ajax({
        url: base_url + "/ajax/index/unvote",
        data: {
            vote_id: vote_id
        },
        success:function(resp) {
            if (resp === '1') {
                window.parent.location = "http://www.facebook.com/" + id_social_page + "?sk=app_" + appId;
            } else {
                //handle error 
            }
        }
    });
}